package org.finesys.chat.core.base.document.loader;

import org.finesys.ai.core.spi.ServiceHelper;
import org.finesys.chat.core.base.document.parser.DocumentParser;
import org.finesys.chat.core.base.document.parser.DocumentParserFactory;

import java.util.Collection;

public class DocumentParserLoader {

    public static DocumentParser loadDocumentParser() {
        Collection<DocumentParserFactory> factories = ServiceHelper.loadFactories(DocumentParserFactory.class);
        if (factories.isEmpty()) {
            return null;
        }
        if (factories.size() > 1) {
            throw new RuntimeException("Conflict: multiple document parsers have been found in the classpath. Please explicitly specify the one you wish to use.");
        } else {
            return factories.iterator().next().create();
        }
    }

}
